package com.ysy.tz.mfg.controller;

import com.ysy.tz.core.selfcomp.CustomerComboBox;
import com.ysy.tz.core.selfcomp.CustomerDialogPane;
import com.ysy.tz.core.selfcomp.PopupTextField;
import com.ysy.tz.core.selfcomp.TextChangeListener;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.mfg.dao.AreaDao;
import com.ysy.tz.mfg.dao.DataListDao;
import com.ysy.tz.mfg.dao.DeliveryDao;
import com.ysy.tz.mfg.dao.EmployeeDao;
import com.ysy.tz.mfg.entity.AreaEntity;
import com.ysy.tz.mfg.entity.DataListEntity;
import com.ysy.tz.mfg.entity.DeliveryEntity;
import com.ysy.tz.mfg.entity.EmployeeEntity;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputControl;
import javafx.stage.Stage;
import org.apache.commons.collections4.CollectionUtils;

public class ArriveFactoryEditController implements Initializable {
    @FXML
    private CustomerDialogPane mainPane;
    @FXML
    private PopupTextField areaName;
    @FXML
    private TextField areaCode;
    @FXML
    private PopupTextField healthInspectorCode;
    @FXML
    private TextField healthInspectorName;
    @FXML
    private PopupTextField suplierCustomerName;
    @FXML
    private TextField suplierCustomerCode;
    @FXML
    private ComboBox deliveryCommitment;
    @FXML
    private CustomerComboBox selfSupplyName;

    public ArriveFactoryEditController() {
    }

    public void initialize(URL location, ResourceBundle resources) {
        this.deliveryCommitment.setItems(FXCollections.observableArrayList(new String[]{"", "Y", "N"}));
        SQLUtils.executeSQL(DataListDao.class, (dataListDao) -> {
            DataListEntity dataListEntity = new DataListEntity();
            dataListEntity.setListTypeCode("SelfSupply");
            ArrayList<DataListEntity> dataListEntities = dataListDao.searchDataListByTypeCode(dataListEntity);
            if (CollectionUtils.isNotEmpty(dataListEntities)) {
                DataListEntity dataListEntity1 = new DataListEntity();
                dataListEntity1.setListCode("");
                dataListEntity1.setListDesc("");
                dataListEntities.add(0, dataListEntity1);
            }

            this.selfSupplyName.setItems(FXCollections.observableArrayList(dataListEntities));
        });
        this.areaName.addTextChangeListener(new TextChangeListener<AreaEntity>() {
            public ObservableList textChanged(TextInputControl textField, String newValue) {
                return (ObservableList)SQLUtils.executeSQLWithReturn(AreaDao.class, (areaDao) -> {
                    try {
                        AreaEntity areaEntity = new AreaEntity();
                        areaEntity.setKeyWord(newValue);
                        ArrayList<AreaEntity> areaEntities = areaDao.listForPage(areaEntity);
                        return FXCollections.observableList(areaEntities);
                    } catch (Exception var4) {
                        var4.printStackTrace();
                        return null;
                    }
                });
            }

            public void PopupChoise(AreaEntity areaEntity) {
                ArriveFactoryEditController.this.areaName.setText(areaEntity.getAreaName());
                ArriveFactoryEditController.this.areaCode.setText(areaEntity.getAreaCode());
            }
        });
        this.healthInspectorCode.addTextChangeListener(new TextChangeListener<EmployeeEntity>() {
            public ObservableList textChanged(TextInputControl textField, String newValue) {
                EmployeeEntity employeeEntity = new EmployeeEntity();
                employeeEntity.setKeyWord(newValue);
                return (ObservableList)SQLUtils.executeSQLWithReturn(EmployeeDao.class, (employeeDao) -> {
                    try {
                        ArrayList<EmployeeEntity> areaEntities = employeeDao.listForPage(employeeEntity);
                        return FXCollections.observableList(areaEntities);
                    } catch (Exception var3) {
                        var3.printStackTrace();
                        return null;
                    }
                });
            }

            public void PopupChoise(EmployeeEntity employeeEntity) {
                ArriveFactoryEditController.this.healthInspectorCode.setText(employeeEntity.getEmployeeCode());
                ArriveFactoryEditController.this.healthInspectorName.setText(employeeEntity.getEmployeeName());
            }
        });
        this.suplierCustomerName.addTextChangeListener(new TextChangeListener<DeliveryEntity>() {
            public ObservableList textChanged(TextInputControl textField, String newValue) {
                return (ObservableList)SQLUtils.executeSQLWithReturn(DeliveryDao.class, (deliveryDao) -> {
                    try {
                        DeliveryEntity deliveryEntity = new DeliveryEntity();
                        deliveryEntity.setKeyWord(newValue);
                        ArrayList<DeliveryEntity> deliveryEntities = deliveryDao.listForPage(deliveryEntity);
                        return FXCollections.observableList(deliveryEntities);
                    } catch (Exception var4) {
                        var4.printStackTrace();
                        return null;
                    }
                });
            }

            public void PopupChoise(DeliveryEntity deliveryEntity) {
                ArriveFactoryEditController.this.suplierCustomerCode.setText(deliveryEntity.getDeliveryCode());
                ArriveFactoryEditController.this.suplierCustomerName.setText(deliveryEntity.getDeliveryName());
            }
        });
    }

    public void hideDialog() {
        Stage stage = (Stage)this.mainPane.getScene().getWindow();
        stage.close();
    }
}
